//
//  CZDatePicker.m
//  网易彩票
//
//  Created by Apple on 15/5/30.
//  Copyright (c) 2015年 itcast. All rights reserved.
//

#import "CZDatePicker.h"
#import "CZKeyboardTool.h"
@interface CZDatePicker()
/**
 *  工具条
 */
@property (nonatomic,weak) CZKeyboardTool *keyboardTool;
/**
 *  日期选择器
 */
@property (nonatomic,weak) UIDatePicker *datePicker;
/**
 *  遮盖按钮
 */
@property (nonatomic,strong) UIButton *cover;
@end

@implementation CZDatePicker

- (UIButton *)cover {
    if (_cover == nil) {
        _cover = [[UIButton alloc] init];
        _cover.backgroundColor = [UIColor clearColor];
        [_cover addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cover;
}

- (void)setCurrentTime:(NSDate *)currentTime {
    _currentTime = currentTime;
    self.datePicker.date = currentTime;
}

- (void)setMinDate:(NSDate *)minDate {
    _minDate = minDate;
    self.datePicker.minimumDate = minDate;
}

- (void)setMaxDate:(NSDate *)maxDate {
    _maxDate = maxDate;
    self.datePicker.maximumDate = maxDate;
}
/**
 *  创建日期选择器
 *
 */
+(instancetype)datePicker{
    return [[CZDatePicker alloc] init];
}


- (id)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        [self setup];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        [self setup];
    }
    return self;
}
/**
 *  创建子控件
 */
- (void)setup{
    // 创建键盘工具条
    CZKeyboardTool *keyboardTool = [CZKeyboardTool keyboardTool];
    [self addSubview:keyboardTool];
    
    __weak typeof(self) weakSelf = self;
    keyboardTool.clickBlock = ^(CZKeyboardToolButtonType buttonType){
        // 隐藏工具条
        [weakSelf dismiss];
        if ([weakSelf.delegate respondsToSelector:@selector(datePicker:didClickBtnType:)]) {
            [weakSelf.delegate datePicker:weakSelf didClickBtnType:buttonType];
        }

    };
    self.keyboardTool = keyboardTool;
    
    // 创建日期选择器
    UIDatePicker *datePicker = [[UIDatePicker alloc] init];
    // 设置时区
    datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh_CN"];
    // 设置时间模式
    datePicker.datePickerMode =   UIDatePickerModeDate;
    // 监听日期改变
    [datePicker addTarget:self action:@selector(dateValueChanged) forControlEvents:UIControlEventValueChanged];
    [self addSubview:datePicker];
    self.datePicker = datePicker;
}
/**
 *  监听日期改变的方法
 */
- (void)dateValueChanged{
    if ([self.delegate respondsToSelector:@selector(datePickerDidValueChanged:)]) {
        [self.delegate datePickerDidValueChanged:self];
    }
}

/**
 *  布局子控件
 */
-(void)layoutSubviews {
    [super layoutSubviews];
    
    CGRect frame = self.keyboardTool.frame;
    frame.size.width = self.frame.size.width;
    frame.size.height = 44;
    frame.origin.x = 0;
    frame.origin.y = 0;
    self.keyboardTool.frame = frame;
    
    CGFloat dateY = CGRectGetMaxY(self.keyboardTool.frame);
    CGFloat dateW = self.frame.size.width;
    CGFloat dateH = 216;
    self.datePicker.frame = CGRectMake(0, dateY, dateW, dateH);
}

/**
 *  显示日期选择器
 */
-(void)show{
    // 获得窗口
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    // 获得窗口的宽高
    CGFloat windowH = CGRectGetHeight(window.frame);
    CGFloat windowW = CGRectGetWidth(window.frame);
    // 设置当前view的高度
    CGFloat height = 260;
    
    // 添加遮盖按钮
    self.cover.frame = CGRectMake(0, 0,windowW , windowH - height);
    [window addSubview:self.cover];
    
    self.frame = CGRectMake(0, windowH, windowW, height);
    [window addSubview:self];
    [UIView animateWithDuration:0.25 animations:^{
        CGRect frame = self.frame;
        frame.origin.y = windowH - height;
        self.frame = frame;
    }];
    
}
/**
 *  隐藏日期选择器
 */
-(void)dismiss{
    // 获得窗口
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    // 获得窗口的高
    CGFloat windowH = CGRectGetHeight(window.frame);
    [UIView animateWithDuration:0.25 animations:^{
        CGRect frame = self.frame;
        frame.origin.y = windowH;
        self.frame = frame;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        // 移除遮盖
        [self.cover removeFromSuperview];
        _cover = nil;
    }];
}

- (void)dealloc {
    NSLog(@"dealloc---datePicker");
}
@end
